package net.service.Impl;

import net.mapper.OrderItemMapper;
import net.mapper.OrderMapper;
import net.mapper.ProductMapper;
import net.mapper.UserMapper;
import net.pojo.*;
import net.service.CartService;
import net.service.OrderService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.UUID;

/**
 * @Author admin
 * @Date 2019/4/9 8:20
 */
@Service
@Transactional
public class OrderServiceImpl implements OrderService {
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    ProductMapper productMapper;
    @Autowired
    OrderItemMapper orderItemMapper;
    @Autowired
    CartService cartService;
    @Override
    public List<Order> findAll() {
        return orderMapper.findAll();
    }

    @Override
    public Order findByOrderId(String id) {
        Order order = orderMapper.findByOrderId(id);
        List<Orderitems> orderitemsList = orderItemMapper.findById(order.getId());
        for (Orderitems orderitems :orderitemsList){
            Product product = productMapper.findById(orderitems.getProduct_id());
            orderitems.setProduct(product);
        }
        order.setOrderitemsList(orderitemsList);
        return order;
    }

    @Override
    public List<Order> search(OrderVo orderVo) {
        List<Order> list = orderMapper.search(orderVo);
        for (Order order :list){
            User user = userMapper.findById(order.getUser_id());
            order.setUser(user);
        }
        return list;
    }

    @Override
    public List<Download> findItemByOid(DateVo dateVo) {
        if (StringUtils.isBlank(dateVo.getYear())){
            Calendar date = Calendar.getInstance();
            dateVo.setYear(String.valueOf(date.get(Calendar.YEAR)));
        }
        if ("0".equals(dateVo.getMonth())){
            dateVo.setMonth(null);
        }
        return orderMapper.findItemByOid(dateVo);
    }

    @Override
    public int createOrder(Order order, User user) {
        List<Cart> cartList = cartService.findByUid(user.getId());
        //设置订单信息,其它信息已经由页面传过来了
        order.setId(UUID.randomUUID().toString());
        order.setPaystate(0);
        order.setUser_id(user.getId());
        //根据购物车信息生成订单条目信息
        List<Orderitems> orderitemList = new ArrayList<>();
        for (Cart cart:cartList){
            Orderitems orderitem = new Orderitems();
            orderitem.setBuynum(cart.getQuantity());
            orderitem.setOrder_id(order.getId());
            orderitem.setProduct_id(cart.getPid());
            orderitemList.add(orderitem);
        }
        //插入订单
        int resultCount =  orderMapper.createOrders(order);
        if (resultCount == 0){
            return 0;
        }

        //插入订单条目
        resultCount = orderMapper.createOrderItems(orderitemList);
        if (resultCount==0){
            return 0;
        }

        //删除购物车内容
        resultCount = cartService.deleteCartByUid(user.getId());
        return resultCount;
    }

    @Override
    public List<Order> findByUid(Integer uid) {
        return orderMapper.findByUid(uid);
    }

    @Override
    public int updatePayState(String out_trade_no, Integer state) {
        return orderMapper.updatePayState(out_trade_no, state);
    }

    @Override
    public int deleteById(String id) {
        return orderMapper.deleteById(id);
    }

}
